import av
import time


def process_rtsp_stream(rtsp_url):
    # 打开RTSP流
    container = av.open(rtsp_url)

    total_decode_time = 0.0
    frame_count = 0

    for frame in container.decode(video=0):
        start_time = time.time()
        img = frame.to_image()  # 将帧转换为PIL图像
        decode_time = time.time() - start_time

        total_decode_time += decode_time
        frame_count += 1

        print(f"Frame {frame_count}: CPU Decode time = {decode_time:.6f} seconds")

        # 如果只处理前100帧，可以取消下面的注释
        # if frame_count >= 100:
        #     break

    print(f"Average decode time: {total_decode_time / frame_count:.6f} seconds")


# 替换为你的RTSP流地址
rtsp_url = "rtsp://127.0.0.1:8554/stream"
process_rtsp_stream(rtsp_url)
